#include "bsitedownloaddialog.h"

#include "ui_bsitedownloaddialog.h"

const QString LUX = "./misc/lux";

BsiteDownloadDialog::BsiteDownloadDialog(QWidget *parent)
    : QDialog(parent), ui(new Ui::BsiteDownloadDialog) {
    ui->setupUi(this);
    ui->le_url->setPlaceholderText("视频地址或者BV号");
    connect(ui->lw_infos, &QListWidget::itemDoubleClicked,
            [=](QListWidgetItem *item) {
                int row = ui->lw_infos->row(item);
                download(m_downloadcmds[row]);
            });
}

BsiteDownloadDialog::~BsiteDownloadDialog() { delete ui; }

void BsiteDownloadDialog::on_pb_info_clicked() {
    if (ui->le_url->text().isEmpty()) return;
    if (ui->lw_infos->count() != 0) ui->lw_infos->clear();
    m_downloadcmds.clear();

    QString url = ui->le_url->text();
    QStringList args;
    args << "-i" << url;

    QProcess *p = new QProcess(this);

    p->start(LUX, args);
    qDebug() << p->program() << " " << p->arguments();
    if (!p->waitForFinished()) return;

    QString res = QString::fromUtf8(p->readAllStandardOutput());
    qDebug() << res;
    // 如果url无效，标准输出会不同
    auto list = res.split("\n");
    qDebug() << list;
    if (not list.at(0).isEmpty()) return;

    get_lux_value(list);
    ui->lb_site->setText(m_site);
    ui->lb_title->setText(m_title);
    ui->lb_type->setText(m_type);
}

void BsiteDownloadDialog::get_lux_value(QStringList list) {
    auto f = [](QString line) -> QString {
        auto right = line.mid(line.indexOf(':') + 1).trimmed();
        return right;
    };
    m_site = f(list.at(1));
    m_title = f(list.at(2));
    m_type = f(list.at(3));

    QStringList lines;
    for (int i = 5; i < list.size(); i++) {
        QString line = list.at(i);
        if (line.isEmpty()) {
            addNewItem(lines);
            lines.clear();
            continue;
        }
        line = list.at(i).trimmed();
        lines.push_back(line);
    }
}

void BsiteDownloadDialog::addNewItem(QStringList lines) {
    if (lines.size() <= 0) return;

    QString text;
    for (int i = 0; i < lines.size(); i++) {
        text += lines[i];
        text += "\n";
    }

    if (!lines.at(0).isEmpty()) {
        QRegularExpression regex("\\[(.*?)\\]");
        QRegularExpressionMatch match = regex.match(lines[0]);
        if (match.hasMatch()) {
            QStringList args;
            args << "-f" << match.captured(1) << ui->le_url->text();
            m_downloadcmds.push_back(args);
        }
    }

    auto item = new QListWidgetItem(ui->lw_infos);
    item->setText(text);
}

void BsiteDownloadDialog::download(QStringList args) {
    QProcess *process = new QProcess(this);
    process->start(LUX, args);

    connect(process, &QProcess::readyReadStandardOutput,
            [=]() { qDebug() << "readyReadStandardOutput triggered"; });

    connect(
        process, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
        [=](int exitcode, QProcess::ExitStatus status) {
            qDebug() << "download finished";
            QMessageBox::information(nullptr, "Info", "Download Finished!!!");
        });
}
